package com.hongxun.budget.ui.fragment.tk;

import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.DMaterialBuyEntityDao;
import com.hongxun.budget.greendao.DMaterialOldEntityDao;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.entity.MaterialInfo;
import com.hongxun.greenentity.tk.DMaterialBuyEntity;
import com.hongxun.greenentity.tk.DMaterialOldEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/8/17.
 */

public class MaterialBuyInfoFragment extends BaseListFragment {
    List<MaterialInfo> datas = new ArrayList<>();
    SuperAdapter<MaterialInfo> adapter;

    @Override
    public SuperAdapter getAdapter() {
        return adapter = new SuperAdapter<MaterialInfo>(R.layout.content_fragment_material_buy_info) {
            @Override
            public void bindView(ViewHolder viewHolder, MaterialInfo d, int i) {
                ((TextView) viewHolder.getView(R.id.lineName)).setText(d.getLineName());
                ((TextView) viewHolder.getView(R.id.name)).setText(d.getName());
                ((TextView) viewHolder.getView(R.id.parameter)).setText(d.getParameter());
                ((TextView) viewHolder.getView(R.id.count)).setText(d.getCount() + "");
                ((TextView) viewHolder.getView(R.id.unit)).setText("(" + d.getUnit() + ")");
                if (d.getWork() != null && d.getWork().equals("拆除")) {
                    ((TextView) viewHolder.getView(R.id.price)).setText("拆除");
                } else
                    ((TextView) viewHolder.getView(R.id.price)).setText(d.getPriceAll() + "元");

            }
        };
    }

    @Override
    public void initView() {
        super.initView();
        initData();
        adapter.setData(datas);
        adapter.setEmptyDataView(R.layout.view_empty);
    }

    void initData() {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        List<DMaterialBuyEntity> buyEntities = session.getDMaterialBuyEntityDao().queryBuilder().where(
                DMaterialBuyEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                , DMaterialBuyEntityDao.Properties.MBDatastate.notEq("删除")
        ).list();
        for (DMaterialBuyEntity d : buyEntities) {
            if (d.getMBPrice() < 0) continue;
            MaterialInfo m = new MaterialInfo();
            m.setCount(d.getMBCount());
            m.setLineName(DBUtils.getLineNameWithPPname(d.getPPGuid()));
            m.setName(d.getMBName());
            m.setParameter(d.getMBParameter());
            m.setPrice(d.getMBPrice());
            m.setPriceAll(d.getMBPriceall());
            m.setUnit(d.getMBUnit());
            datas.add(m);
        }
        List<DMaterialOldEntity> oldEntities = session.getDMaterialOldEntityDao().queryBuilder().where(
                DMaterialOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                , DMaterialOldEntityDao.Properties.MBDatastate.notEq("删除")
        ).list();
        for (DMaterialOldEntity d : oldEntities) {
            MaterialInfo m = new MaterialInfo();
            m.setCount(d.getMBCount());
            m.setLineName(DBUtils.getLineNameWithPPname(d.getPPGuid()));
            m.setName(d.getMBName());
            m.setParameter(d.getMBParameter());
            m.setUnit(d.getMBUnit());
            m.setWork("拆除");
            //new
            datas.add(m);
        }
    }
}
